package BLL.User;


import BIZ.UserBiz;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet(name = "switchType", value = "/switchType")
public class switchType extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public switchType() {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();

        String remainday = null;
        UserBiz userBiz = new UserBiz();
        //从表单获取数据
        String count = new String(request.getParameter("count").getBytes(StandardCharsets.UTF_8));


        //从session获取数据
        String Type = new String(request.getParameter("Type").getBytes(StandardCharsets.UTF_8));
        String UserName = (String) session.getAttribute("UserName");
        String carid1 = (String) session.getAttribute("CaiNumber1");
        String carid2 = (String) session.getAttribute("CaiNumber2");
        String carid3 = (String) session.getAttribute("CaiNumber3");
        System.out.println(Type+UserName+carid1+carid2+carid3+count);
        //判断用户类型
        if (Type.equals("0")) {
            //获取用户剩余 天 数
            remainday = (String) session.getAttribute("remainingDays");

            //进行充值n 次/天
            String x = userBiz.addremain1(count, UserName, remainday, carid1, carid2, carid3, Type);

            //获取充值后的剩余余额
            String y = userBiz.addremain2(count, UserName, remainday, carid1, carid2, carid3, Type);

            //给session新的remainingDays
            session.setAttribute("remainingDays", x);

            //给session新的余额
            session.setAttribute("remainingDays", y);

        }
        if (Type.equals("1")) {
            //获取用户剩余 次 数
            remainday = (String) session.getAttribute("remainDegree");

            //进行充值n 次/天
            String x = userBiz.addremain1(count, UserName, remainday, carid1, carid2, carid3, Type);

            //获取充值后的剩余余额
            String y = userBiz.addremain2(count, UserName, remainday, carid1, carid2, carid3, Type);

            //给session新的remainDegree
            session.setAttribute("remainDegree", x);

            //给session新的余额
            session.setAttribute("remainingDays", y);
        }


    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
